<?php

/**
 * Page
 *
 * @package
 * @author kubicek
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
class Page extends Plugin
{
	public $plugin_name = "Page"; //name of plugin
	public $plugin_short_desc = "Static pages"; //short description of plugin
	public $plugin_long_desc = "Static pages with no dynamic PHP for something like About us"; //longer description - what does plugin do
	public $version = "1.2"; //version of plugin
	public $has_sidebar = false; //does this plugin have sidebar -> function generate_sidebar()
	public $has_footer = false; //does this plugin have footer -> function generate_footer()
	public $needs_install = true;

	/**
	 * Constructor
	 */
	function __construct()
	{
	}

	public function start()
	{
		global $core;

		$core->router_id = PAGE_DEFAULT_ID;
		$this->show_page();
	}


	/**
	 * Page::show_page()
	 * Show page with $core->router_id
	 * @return
	 */
	public function show_page()
	{
		global $core,$theme;

		if($core->router_id)
		{
			$query = mysql_query("SELECT * FROM ".DB_PREFIX."yaps_page WHERE id = ".$core->router_id);
			if(mysql_num_rows($query) > 0)
			{
				$page = mysql_fetch_array($query);
				$theme->set_theme($page['theme']);
				$theme->set_title($page['title']);

				echo stripslashes($page['text']);
			}
			else
			{
				$core->err('There is no page with this ID');
				$core->refresh(WEB_URL);
			}
		}
	}

	/**
	 * Page::do_get_stuff()
	 * Handle $_GET requests
	 * @return
	 */
	private function do_get_stuff()
	{
		global $core,$theme;

		//WE WILL EDIT EXISTING ENTRY
		if($core->get('edit'))
		{
			echo '<h3>Edit page</h3>';

			$query = mysql_query("SELECT * FROM ".DB_PREFIX."yaps_page WHERE id = ".$core->get('edit'));
			if(mysql_num_rows($query) > 0)
			{
				$entry = mysql_fetch_array($query);

				echo '<form action="" method="post">
							<input type="text" name="title" value="',stripslashes($entry['title']),'" size="60" />
							<textarea name="page_entry" id="page_entry" cols="75" rows="15">',stripslashes($entry['text']),'</textarea><br/>
							Page theme: <select name="theme">
							';

				$themes = $theme->get_theme_list();

				for($i = 0; $i < sizeof($themes); $i++)
				{
					$default_select = ($themes[$i] == $entry['theme']) ? 'selected="true"' : '';
					$default_name = ($themes[$i] == $theme->theme_name) ? ' - Default' : '';
					echo '<option value="'.$themes[$i].'" '.$default_select.'>',$themes[$i],$default_name,'</option>';
				}

				$query = mysql_query("SELECT * FROM ".DB_PREFIX."yaps_routing_table WHERE plugin = 'page' AND action = 'show' AND id_for_action = '".$entry['id']."'");
				if(mysql_num_rows($query) > 0) $url = mysql_fetch_array($query);

				echo '
								</select><br />
							Fixed url: <strong>'.WEB_URL.'</strong><input type="text" name="url" value="'.$url['url'].'"/> - this option is non mandatory<br />
							<input type="submit" value="Edit this page" />
							<input type="hidden" name="id" value="',$entry['id'],'" />
							</form>';
			}
			else
			{
				$core->err('Bad ID of page! Check your DB table `su_yaps_page`.');
			}
		}
		elseif($core->get('delete')) //LETS DELETE SOMETHING
		{
			$query = mysql_query("DELETE FROM ".DB_PREFIX."yaps_page WHERE id = ".$core->get('delete'));
			$core->msg('Page with ID '.$core->get('delete').' has been deleted.');
			$theme->js('$(location).attr(\'href\',\'admin-pluginsettings?plugin=page\');');
		}
	}

	/**
	 * Page::do_post_stuff()
	 * Handles $_POST requests
	 * @return
	 */
	private function do_post_stuff()
	{
		global $core,$theme;

		//EDITING EXISTING ENTRY
		if($core->post('id'))
		{
			$query = mysql_query("UPDATE ".DB_PREFIX."yaps_page SET
			text = '".$core->post('page_entry')."',
			title = '".$core->post('title')."',
			theme = '".$core->post('theme')."'
			WHERE id = ".$core->post('id'));

			//do we have url? Lets add it to the routing table
			if($core->post('url'))
			{
				$core->add_to_routing_table($core->post('url'),'page','show',$core->post('id'));
			}

			if($query) $core->msg('Your page ID '.$core->post('id').' has been updated.');
			else $core->err('Oops, can`t edit your page ID '.$core->post('id').', please check your DB table `su_yaps_page`.');
			$theme->js('$(location).attr(\'href\',\'admin-pluginsettings?plugin=page\');');
		}
		elseif($core->post('page_entry')) //LETS ADD SOMETHING
		{
			$query = mysql_query("INSERT INTO su_yaps_page VALUES(
			'0',
			'".$core->post('title')."',
			'".$core->post('page_entry')."',
			'".$core->post('theme')."'
			)");

			//do we have url? Lets add it to the routing table
			if($core->post('url'))
			{
				$core->add_to_routing_table($core->post('url'),'page','show',mysql_insert_id());
			}

			if($query) $core->msg('Your page has been added.');
			else $core->err('Oops, can`t insert your page, please check your DB table `su_yaps_page`.');
			$theme->js('$(location).attr(\'href\',\'admin-pluginsettings?plugin=page\');');
		}
	}

	/**
	 * Page::admin_settings()
	 * @return
	 */
	public function admin_settings()
	{
		global $core,$theme;

		if($core->is_plugin('wysiwyg'))
		{
			$wysiwyg = $core->load_plugin('wysiwyg');
			$wysiwyg->create_wysiwyg('page_entry');
		}

		$theme->include_jquery(false);
		//$theme->js('$("#ac").accordion({ autoHeight: false });');

		echo '<div id="ac">';

		$this->do_get_stuff();
		$this->do_post_stuff();

		echo '<h3>Add new static page</h3>
					<form action="" method="post">
						Title: <input type="text" name="title" size="60" /><br />
						<textarea name="page_entry" id="page_entry" cols="75" rows="15"></textarea><br />
						Page theme: <select name="theme">
							';

		$themes = $theme->get_theme_list();

		for($i = 0; $i < sizeof($themes); $i++)
		{
			$default_select = ($themes[$i] == $theme->theme_name) ? 'selected="true"' : '';
			$default_name = ($themes[$i] == $theme->theme_name) ? ' - Default' : '';
			echo '<option value="'.$themes[$i].'" '.$default_select.'>',$themes[$i],$default_name,'</option>';
		}

		echo '
						</select><br />
						Fixed url: <strong>'.WEB_URL.'</strong><input type="text" name="url" /> - this option is non mandatory<br />
						<input type="submit" value="Create new static page" />
					</form>
					<h3>Static pages</h3>';

		$query = mysql_query("SELECT * FROM ".DB_PREFIX."yaps_page ORDER BY title ASC");
		if(mysql_num_rows($query) > 0)
		{
			echo '<table class="nice">
						<tr><th>ID</th><th>Title</th><th>Theme</th><th>Options</th></tr>';

			while($item = mysql_fetch_array($query))
			{
				echo '<tr>
							<td>',$item['id'],'</td>
							<td><a href="page-show-',$item['id'],'-',$core->encode_url($item['title']),'">',$item['title'],'</a></td>
							<td>',$item['theme'],'</td>
							<td><a href="admin-pluginsettings?plugin=page&amp;edit=',$item['id'],'">',$theme->icon('pencil.png','Edit'),'</a>
									<a href="admin-pluginsettings?plugin=page&amp;delete=',$item['id'],'">',$theme->icon('cancel.png','Delete'),'</a>
							</td>
							</tr>';
			}

			echo '</table>';
		}
		else
		{
			echo 'Nothing in there ;)';
		}

		echo '</div>';
	}

	/**
	 * Page::install()
	 * Installation of this plugin
	 * @return
	 */
	public function install()
	{
		$create_table = mysql_query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."yaps_page` (
		   `id` int(11) NOT NULL AUTO_INCREMENT,
		   `title` text COLLATE utf8_czech_ci NOT NULL,
		   `text` text COLLATE utf8_czech_ci NOT NULL,
		   `theme` text COLLATE utf8_czech_ci NOT NULL,
		   PRIMARY KEY (`id`)
		   ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1");

		$insert_data = mysql_query("INSERT INTO `".DB_PREFIX."yaps_page` (`id`, `title`, `text`, `theme`) VALUES
		   (1, 'About me - first testing page', 'Yeah, this is me :)','earthlingtwo')");

		$insert_data_1 = mysql_query('INSERT INTO '.DB_PREFIX.'yaps_config VALUES(\'plugin_page_installed\',\'true\')');
		$insert_data_2 = mysql_query('INSERT INTO '.DB_PREFIX.'yaps_config VALUES(\'page_default_id\',\'1\')');

		if($create_table && $insert_data && $insert_data_1) return true;
		else return false;
	}
}


?>